package com.app.yc.ifapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.Toast;

import com.app.yc.ifapp.asynctask.WebApiRequest;
import com.app.yc.ifapp.entity.ApiResult;
import com.app.yc.ifapp.entity.UserInfo;
import com.app.yc.ifapp.utils.ApiUtils;
import com.app.yc.ifapp.utils.EncryptUtils;
import com.app.yc.ifapp.utils.StringUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.net.URLEncoder;

public class WelcomeActivity extends Activity {

    String user_No;
    String pwd;
    String url;

    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        //获取并缩放右箭头
        Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.yc_logo);
        drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * 0.5),
                (int) (drawable.getIntrinsicHeight() / 0.5));
        //获取到控件
        ImageView iv = (ImageView)findViewById(R.id.image_yc_logo);
        iv.setImageDrawable(drawable);

        SharedPreferences sp = getSharedPreferences("login_user",
                Context.MODE_PRIVATE);
        user_No = sp.getString("user_no", StringUtils.EMPTY);
        pwd = sp.getString("password", StringUtils.EMPTY);
        url = sp.getString("url", StringUtils.EMPTY);

        if (StringUtils.isNullOrEmpty(user_No) || StringUtils.isNullOrEmpty(pwd) ||
                StringUtils.isNullOrEmpty(url)) {
            //没有找到保存的登录数据，跳转到登录界面
            intent = new Intent(WelcomeActivity.this, LoginActivity.class);
            Handler handler = new Handler();
            //当计时结束,跳转至主界面
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(intent);
                    WelcomeActivity.this.finish();
                }
            }, 3000);
        } else {
            //找到了登录信息，尝试登录
            WebApiRequest request = new WebApiRequest();
            request.setAsyncTaskCallBack(new WebApiRequest.AsyncTaskCallBack() {
                @Override
                public void onTaskOver(String result) {

                    if (TextUtils.isEmpty(result)) {
                        Toast.makeText(WelcomeActivity.this, R.string.error_api_fail,
                                Toast.LENGTH_SHORT).show();
                    } else {
                        try {
                            Gson gson = new Gson();
                            ApiResult<UserInfo> apiResult = gson.fromJson(result,
                                    new TypeToken<ApiResult<UserInfo>>() {
                                    }.getType());

                            if (apiResult == null) {
                                Toast.makeText(WelcomeActivity.this,
                                        R.string.error_api_fail,
                                        Toast.LENGTH_SHORT).show();
                            } else {

                                if (apiResult.getResult() && apiResult.getData() != null) {
                                    intent = new Intent(WelcomeActivity.this, MainActivity.class);
                                    //把附加的数据放到意图当中
                                    intent.putExtra("user", apiResult.getData());
                                    intent.putExtra("url", url);
                                } else {
                                    Toast.makeText(WelcomeActivity.this, apiResult.getMessage(),
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(WelcomeActivity.this, R.string.error_system,
                                    Toast.LENGTH_SHORT).show();
                        }
                    }

                    if (intent == null) {
                        intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                    }

                    Handler handler = new Handler();
                    //当计时结束,跳转至主界面
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(intent);
                            WelcomeActivity.this.finish();
                        }
                    }, 2000);
                }
            });

            try {
                //构造请求登录参数
                String params = "?empStrId" + "=" +
                        URLEncoder.encode(user_No, "UTF-8") +
                        "&userPwd" + "=" +
                        URLEncoder.encode(EncryptUtils.SHA.SHA256(pwd), "UTF-8");

                request.execute(url + ApiUtils.LOGIN + params,
                        StringUtils.EMPTY);
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(WelcomeActivity.this, R.string.error_api_fail,
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
}
